import axios from 'axios';
import {baseImgPath} from './env';
/* ajax-get */
export const fetch = (url,data={}) =>{
	return new Promise((resolve,reject) => {
		axios({
			method:'get',
			url:url,
			params:data,
			headers:{
				'X-Requested-With': 'XMLHttpRequest'
			},
		}).then(resp=>{
			resolve(resp)
		}).catch(err=>{
			reject(err);
		})  
	})
}

/* json-post */
export const postJson = (url,data={}) =>{
	return new Promise((resolve,reject)=>{
		axios({
			method:'post',
			url:url,
			data:data,
			headers:{
				'X-Requested-With': 'XMLHttpRequest'
			},
		}).then(resp=>{
			resolve(resp)
		},reject)
	})
}
/* json-post */
export const postImg = (url,data={}) =>{
	return new Promise((resolve,reject)=>{
		axios({
			method:'post',
			url:url,
			data:data,
			headers:{
				'X-Requested-With': 'XMLHttpRequest'
			},
			// onUploadProgress:function(progressEvent){ //原生获取上传进度的事件
            //     if(progressEvent.lengthComputable){
			// 		globalEvent.$emit('GetUpLoading',progressEvent);
            //     }
			// }
		}).then(resp=>{
			resolve(resp)
		},reject)
	})
}

/* ajax-post */
export const postData = (url,data={}) =>{
	return new Promise((resolve,reject)=>{
		axios({
			method:'post',
			url:url,
			headers:{
				'Content-type': 'application/x-www-form-urlencoded'
			},
			data:data,
			transformRequest: [function (data) {
				let ret = ''
				for (let it in data) {
				  ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
				}
				return ret
			}]
		}).then(resp=>{
			resolve(resp)
		},reject)
	})
}

/**ajax-patch */
export const postPatch = (url,data={}) =>{
	return new Promise((resolve,reject)=>{
		axios({
			method:'patch',
			url:url,
			data:data,
			headers:{
				'X-Requested-With': 'XMLHttpRequest'
			}
		}).then(resp=>{
			resolve(resp)
		},reject)
	})
}

export const deleteAjax = (url,data={}) =>{
	return new Promise((resolve,reject)=>{
		axios({
			method:'delete',
			url:url,
			data:data,
			headers:{
				'X-Requested-With': 'XMLHttpRequest'
			}
		}).then(resp=>{
			resolve(resp)
		},reject)
	})
}

/* ajax-post */
export const  patchForm= (url,data={}) =>{
	return new Promise((resolve,reject)=>{
		axios({
			method:'patch',
			url:url,
			headers:{
				'Content-type': 'application/x-www-form-urlencoded'
			},
			data:data,
			transformRequest: [function (data) {
				let ret = ''
				for (let it in data) {
				  ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
				}
				return ret
			}]
		}).then(resp=>{
			resolve(resp)
		},reject)
	})
}


/* ajax-post */
export const postFile = (url,data={}) =>{
	return new Promise((resolve,reject)=>{
		axios({
			method:'post',
			url:url,
			headers:{
				'Content-Type':'multipart/form-data'
			},
			data:data,
			// transformRequest: [function (data) {
			// 	let ret = ''
			// 	for (let it in data) {
			// 	  ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
			// 	}
			// 	return ret
			// }]
		}).then(resp=>{
			resolve(resp)
		},reject)
	})
}